package com.sweetsourfish.crm.activity.web;

import com.sweetsourfish.crm.activity.model.ActivityRemark;
import com.sweetsourfish.crm.activity.service.ActivityRemarkService;
import com.sweetsourfish.crm.commons.model.Constants;
import com.sweetsourfish.crm.commons.model.ResponseResult;
import com.sweetsourfish.crm.commons.utils.DateUtils;
import com.sweetsourfish.crm.commons.utils.UUIDUtils;
import com.sweetsourfish.crm.settings.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @Description: 市场业务备注控制层
 * @Version: 1.0
 */

@Controller
@RequestMapping("/activity")
public class ActivityRemarkController {

    //注入Service
    @Autowired
    private ActivityRemarkService activityRemarkService;

    /**
     * Description: 查询结果
     *
     * @param activityId 根据id查询详细备注
     * @return 查询结果
     */
    /*通过 activityid 获取  备注集合*/
    @RequestMapping("/api/remarkList")
    @ResponseBody
    public ResponseResult selectActivityById(String activityId) {
        //产生响应结果
        ResponseResult result = new ResponseResult();

        List<ActivityRemark> remarkList = activityRemarkService.queryActivitiesById(activityId);

        result.setCode(Constants.STATUS_OK);
        result.setData(remarkList);
        return result;
    }

    /**
     * Description: 添加市场活动备注
     * @param activityRemark 实体类参数
	 * @param session  用来与用户会话
     * @return 返回添加结果
     */
    @RequestMapping("/api/addActivityRemarks")
    @ResponseBody
    public ResponseResult addActivityRemarks(ActivityRemark activityRemark, HttpSession session) throws Exception {
        //产生会话
        User user = (User) session.getAttribute(Constants.SESSION_USER);

        //封装参数
        activityRemark.setId(UUIDUtils.getUUID());
        activityRemark.setCreateTime(DateUtils.formatDateTime(new Date()));
        activityRemark.setCreateBy(user.getId());
        activityRemark.setEditFlag(Constants.REMARK_EDIT_FLAG_NO_EDITED);
        ResponseResult result = new ResponseResult();

        //插入添加操作判断
        try {
            int rows = activityRemarkService.addActivityRemark(activityRemark);
            if (rows > 0) { // 插入成功则
                result.setCode(Constants.STATUS_OK);
                result.setData(activityRemark); // 将备注也传到前端响应到页面
            } else { // 插入失败，服务器端出了问题，为了不影响顾客体验，最好不要直接说出问题
                result.setCode(Constants.STATUS_ERROR);
                result.setMsg("(添加失败)系统繁忙，请稍后重试...");
            }
        } catch (Exception e) {//发生某些异常后 捕捉后返回此信息
            e.printStackTrace();
            result.setCode(Constants.STATUS_ERROR);
            result.setMsg("系统繁忙，请稍后重试...");

        }
        return result;
    }

    /**
     * 通过备注id删除市场活动备注
     * @param removeId 市场活动备注id
     * @return 响应到前端的信息
     */
    @RequestMapping("/api/deleteActivityRemark")
    @ResponseBody
    public ResponseResult deleteActivityRemark(String removeId){
        //响应结果
        ResponseResult result = new ResponseResult();

        try {
                int rows = activityRemarkService.removeActivityRemark(removeId);
                if (rows > 0) { // 删除成功则
                    result.setCode(Constants.STATUS_OK);

                } else { // 删除失败，服务器端出了问题，为了不影响顾客体验，最好不要直接说出问题
                    result.setCode(Constants.STATUS_ERROR);
                    result.setMsg("(删除失败)系统繁忙，请稍后重试...");
                }
            } catch (Exception e) {//发生某些异常后 捕捉后返回此信息
                 e.printStackTrace();
                 result.setCode(Constants.STATUS_ERROR);
                 result.setMsg("系统繁忙，请稍后重试...");

            }
                return result;
    }

    /**
     * Description: 修改备注内容
     * @param activityRemark 实体类数据
	 * @param session  用来获取当前用户对象
     * @return 修改结果
     */
    @RequestMapping("/api/updateActivityRemark")
    @ResponseBody
    public ResponseResult updateActivityRemark(ActivityRemark activityRemark, HttpSession session) throws Exception {
        //产生会话  获取当前的user对象
        User user = (User) session.getAttribute(Constants.SESSION_USER);

        //封装参数
        activityRemark.setEditFlag(Constants.REMARK_EDIT_FLAG_NO_EDITED);
        activityRemark.setCreateTime(DateUtils.formatDateTime(new Date()));
        activityRemark.setCreateBy(user.getId());
        ResponseResult result = new ResponseResult();

        //修改操作判断
        try {
            int rows = activityRemarkService.addActivityRemark(activityRemark);
            if (rows > 0) { // 修改成功则
                result.setCode(Constants.STATUS_OK);
                result.setData(activityRemark); // 将备注也传到前端响应到页面
            } else { // 修改失败，服务器端出了问题，为了不影响顾客体验，最好不要直接说出问题
                result.setCode(Constants.STATUS_ERROR);
                result.setMsg("(修改失败)系统繁忙，请稍后重试...");
            }
        } catch (Exception e) {//发生某些异常后 捕捉后返回此信息
            e.printStackTrace();
            result.setCode(Constants.STATUS_ERROR);
            result.setMsg("系统繁忙，请稍后重试...");

        }
        return result;
    }
}
